import CURDHelper from '@/utils/CURDHelper'
import PageConfig from '@/utils/pageConfig'
import { getBtnCtr } from '@/utils/BtnControl'
import Query from '@/components/Query';
import { isNumber } from '@/utils/validate';
import { Tooltip, VxeUI } from 'vxe-pc-ui';
import XEUtils from 'xe-utils';
export default {
  name: 'CertificateList',
  components: {
    Query
  },
  data() {
    const gltxItemRender = {
      name: 'VxeSelect',
      props: {
        filterable: true,
      },
      options: [],
    }
    const khItemRender = {
      name: 'VxeSelect',
      props: {
        filterable: true,
      },
      options: []
    }
    const lcmbItemRender = {
      name: 'VxeSelect',
      props:{
        filterable: true,
      },
      options: [],
      
    }
    const qdItemRender = {
      name: 'VxeSelect',
      props:{
        filterable: true,
        clearable: true,
      },
      options: [] 
    }
    return {
      gltxItemRender,
      khItemRender,
      lcmbItemRender,
      qdItemRender,
      componentSize: PageConfig.ComponentSize,
      duration: PageConfig.Duration,
      btnPromission: '',
      editModal: false,
      QueryConditions: [],
      BaseChildApiUrl: '/api/Xmkhgltx',
      editRow: undefined,
      CalendarView: false,
      QueryConfig: [{
        title: '客户名称',
        field: 't2.khmc',
        type: 'input',
        value: '',
        placeholder: '请输入客户名称以查询',
      },{
        title: '证书状态',
        field: 'zszt',
        type: 'select',
        value: '',
        placeholder: '请输入选择证书状态以查询',
        options: [
          { label: '有效', value: '有效' },
          { label: '暂停', value: '暂停' },
          { label: '撤销', value: '撤销' }
        ]
      },{
        title: '体系名称',
        field: 'txid',
        type: 'checkbox',
        value: '',
        chkoptions:[],
        placeholder: '请输入体系名称以查询',
      }],
      PagerConfig: {
        align: 'center',
        pageSize: 25,
        total: 0,
        currentPage: 1,
        pageSizes: [10, 25, 50, 100, 200],
      },
      ChildGridOptions: {
        id: 'CertificateChildGrid',
        border: true,
        stripe: true,
        loading: false,
        size: PageConfig.ComponentSize,
        height: 700,
        columnConfig: {
          resizable: true
        },
        rowConfig: {
          isHover: true,
          isCurrent: true
        },
        exportConfig: {
        },
        customConfig: {
          mode: 'drawer',
          storage: true
        },
        columns: [
          { type: 'seq', width: 50 },
          { field: 'khmc', title: '客户名称', minWidth: 150, showOverflow: true },
          { field: 'txmc', title: '体系名称', minWidth: 150, showOverflow: true },
          { field: 'txgg', title: '体系规格', minWidth: 120, showOverflow: true },
          { field: 'zsh', title: '证书号', minWidth: 120, showOverflow: true },
          { field: 'rzbz', title: '认证标准', minWidth: 120, showOverflow: true },
          { field: 'zslb', title: '证书类别', minWidth: 120, showOverflow: true },
          { field: 'zsrq', title: '证书日期', minWidth: 120, showOverflow: true , formatter: ({cellValue}) => XEUtils.toDateString(cellValue,'yyyy-MM-dd')},
          { field: 'zsyxq', title: '证书有效期', minWidth: 120, showOverflow: true , formatter: ({cellValue}) => XEUtils.toDateString(cellValue,'yyyy-MM-dd')},
          { field: 'zszt', title: '证书状态', minWidth: 120, showOverflow: true, slots: { default: 'zsztRender' }},
          { field: 'zsjg', title: '证书机构', minWidth: 120, showOverflow: true },
          { field: 'dqshlx', title: '审核阶段', minWidth: 120, showOverflow: true },
          { field: 'mcshrq', title: '初审末次时间', minWidth: 150, showOverflow: true },
          { field: 'xcshrq', title: '下次审核提醒', minWidth: 150, showOverflow: true },
          { field: 'operation', title: '操作', fixed: 'right', width: 60, slots: { default: 'operation' } },
        ],
        data: []
      },
      FormOptions: {
        'title-width': 100,
        border: true,
        titleBackground: true,
        data: {
          id: -1,
          lcmb:undefined,
          createid: '',
          createtime: '',
          createname: '',
          updateid: '',
          updatetime: '',
          updatename: '',
          khid: undefined,
          txid: undefined,
          erpbh: '',
          sf:'',
          sxq: '',
          shlx: '',
          rzfw: '',
          txrs: '',
          csje: undefined,
          jsje: undefined,
          htqdrq: undefined,
          bz: '',
          htje:undefined,
          sfdcs:undefined,
          dcslb:undefined,
          htlx:undefined,
          htdz:undefined,
          qdid:undefined
        },
        rules: {
          txid: [{ required: true, message: '请选择体系名称', trigger: 'blur' }],
          lcmb: [{ required: true, message: '请选择流程模板', trigger: 'blur' }],
          erpbh: [{ required: true, message: '请填写合同号', trigger: 'blur' }],
          khid: [{ required: true, message: '请选择客户信息', trigger: 'blur' }],
          shlx: [{ required: true, message: '请选择审核类型', trigger: 'blur' }],
          rzfw: [{ required: true, message: '请选择认证范围', trigger: 'blur' }],
          txrs: [{ required: true, message: '请输入体系人数', trigger: 'blur' ,validator: this.ValidatorNumber}],
          csje: [{ required: true, message: '请输入初审金额', trigger: 'blur' ,validator: this.ValidatorNumber}],
          htje: [{ required: true, message: '请输入合同金额', trigger: 'blur' ,validator: this.ValidatorNumber}],
          jsje: [{ required: true, message: '请输入监审金额', trigger: 'blur' ,validator: this.ValidatorNumber}],
          htqdrq: [{ required: true, message: '请选择合同日期', trigger: 'blur' }],
        },
        items: [
          { field: 'txid', title: '体系名称', span: 12, itemRender: gltxItemRender, placeholder: '请选择体系名称' },
          { field: 'lcmb', title: '流程模板', span: 12, itemRender: lcmbItemRender, placeholder: '请选择流程模板' },
          { field: 'khid', title: '客户名称', span: 12, itemRender: khItemRender, placeholder: '请选择客户名称' },
          { field: 'erpbh', title: '合同号', span: 12, itemRender:{name:'VxeInput'}, placeholder: '请输入合同号' },
          { field: 'htlx', title: '合同类型', span: 12, itemRender: { name: 'VxeSelect', 
            options: [{ label: '初审', value: '初审' }, { label: '监一', value: '监一' },
              { label: '监二', value: '监二'},{ label: '再认证', value: '再认证'}] }, placeholder: '请选择合同类型' },
          { field: 'sf', title: '省份', span: 12, itemRender:{name:'VxeInput'}, placeholder: '请输入省份' },
          { field: 'sxq', title: '区域', span: 12, itemRender:{name:'VxeInput'}, placeholder: '请输入区域' },
          { field: 'shlx', title: '审核类型', span: 12, itemRender: { name: 'VxeSelect', 
            options: [{ label: '初认证', value: '初认证' }, { label: '监一', value: '监一' },
              { label: '监二', value: '监二'},{ label: '再认证', value: '再认证'},
              { label: '扩大', value: '扩大'},{ label: '补充', value: '补充'},,{ label: '不符合现场验证', value: '不符合现场验证'}] }, placeholder: '请选择审核类型' },
          { field: 'txrs', title: '体系人数', span: 12, placeholder: '请输入体系人数', 
            itemRender: { name: 'VxeInput', clearable: true, placeholder: '请输入体系人数' } },
          { field: 'qdid', title: '渠道名称', span: 12, itemRender: qdItemRender },
          { field: 'rzfw', title: '认证范围', span: 24, itemRender: {  name: 'VxeTextarea', maxlegnth: 500, clearable: true, 
            showWordCount: true }},
          { field: 'csje', title: '初审金额', span: 12, placeholder: '请输入初审金额',
            itemRender: { name: 'VxeInput',  clearable: true, placeholder: '请输入初审金额' ,
              events:{ change:e =>{this.FormOptions.data.htje = this.FormOptions.data.csje}}} },
          { field: 'jsje', title: '监审金额', span: 12, placeholder: '请输入监审金额',
            itemRender: { name: 'VxeInput',  clearable: true, placeholder: '请输入监审金额' } },
          { field: 'htje', title: '合同金额', span: 12, placeholder: '请输入合同金额',
              itemRender: { name: 'VxeInput',  clearable: true, placeholder: '请输入合同金额' } }, 
          { field: 'htqdrq', title: '合同日期', span: 12, itemRender: { name: 'VxeDatePicker', type: 'date', valueFormat: 'YYYY-MM-DD', 
            placeholder: '请选择签单日期' } },
          { field: 'sfdcs', title: '是否多场所', span: 12, itemRender: { name: 'VxeSelect', 
            options: [{ label: '', value: '' },{ label: '是', value: '是' }, { label: '否', value: '否' }] }, placeholder: '请选择是否多场所'},
          { field: 'dcslb', title: '多场所类别', span: 12, itemRender: { name: 'VxeSelect', 
             options: [{ label: '', value: '' },{ label: '延伸', value: '延伸' }, { label: '支持', value: '支持' }
              , { label: '支持、延伸', value: '支持、延伸' }, { label: '集团', value: '集团' }
             ],clearable: true }, placeholder: '请选择多场所类别'},
          { field: 'zydm', title: '专业代码', span: 24, itemRender: { name: 'VxeInput', clearable: true, placeholder: '请输入专业代码'}}, 
          { field: 'htdz', title: '合同地址', span: 24, itemRender: { name: 'VxeInput', clearable: true, placeholder: '请输入合同地址'}}, 
          { field: 'bz', title: '备注', span: 24, itemRender: { name: 'VxeTextarea', maxlegnth: 500, clearable: true, showWordCount: true }, 
            placeholder: '请输入备注' },
          {
            align: 'center',
            span: 24,
            // itemRender: {
            //   name: 'VxeButtonGroup',
            //   options: [{
            //     type: 'submit', content: '提交', status: 'primary'
            //   }, {
            //     type: 'reset', content: '重置'
            //   }]
            // }
            slots:{default:'action'}
          }
        ]
      },
      editModal:false
    }
  },
  created() {
    // 动态头部按钮
    this.btnPromission = getBtnCtr(
      this.$route.path,
      this.$store.getters.userPermission
    )
    this.GetTxOptions();
    this.GetKhOptions();
    this.GetQdOptions();
  },
  methods: {
    GetQdOptions() {
      let request = {
        isPaging: false,
        pageSize: 1,
        pageIndex: 1,
        queryConditions: [],
        orderByConditions: []
      }
      CURDHelper.Query(request, '/api/Xmjcqdxx')
        .then(res => {
          this.qdItemRender.options = [];
          res.data.forEach(item => {
            this.qdItemRender.options.push({
              value: item.id,
              label: item.qdmc
            })
          })
        }).catch(err => {
          VxeUI.modal.message({content: `${err}`,status: 'error',duration: this.duration})
        })
    },
    handleQuery(e) {
      this.QueryConditions = e;
      this.DoChildQuery(e);
    },
    DoChildQuery(e) {
      let queryConditions = e || [];
      let request = {
        isPaging: true,
        pageSize: this.PagerConfig.pageSize,
        pageIndex: this.PagerConfig.currentPage,
        queryConditions: queryConditions,
        orderByConditions: [{
          column: 't2.khmc',
          condition: 'asc'
        }]
      }
      CURDHelper.Query(request, this.BaseChildApiUrl)
        .then(res => {
          this.ChildGridOptions.data = res.data;
          this.PagerConfig.total = res.totalNumber;
        }).catch(err => {
          VxeUI.modal.message({ content: `${err}`, status: 'error', duration: this.duration })
        })
    },
    handlePageChange() {
      this.DoChildQuery(this.QueryConditions);
    },
    handleCreate(row) {
      this.DoCreate(row);
    },
    DoCreate(row) {
      let query = [
        { column: 'khid', condition: 0, content: row.khid },
        { column: 'txid', condition: 0, content: row.txid },
        { column: 'zsh', condition: 0, content: row.zsh },
      ]
      let request = {
        isPaging: false,
        pageSize: 1,
        pageIndex: 1,
        queryConditions: query,
        orderByConditions: []
      }
      CURDHelper.Query(request, '/api/xmglxmxx')
        .then(res => {
          if (res.data && res.data.length > 0) {
            console.log(res.data)
            this.FormOptions.data = res.data[0];
            this.FormOptions.data.erpbh = '';
            this.FormOptions.data.shlx = '';
            this.FormOptions.data.shzt = 'N';
            this.FormOptions.data.shr = null;
            this.FormOptions.data.shsj = null;
            this.FormOptions.data.pczt = 'N';
            this.FormOptions.data.pcshr = null;
            this.FormOptions.data.pcshsj = null;
            this.FormOptions.data.pcshzt = 'N';
            this.FormOptions.data.xmzt = 'N';
            this.FormOptions.data.xmztqrr = null;
            this.FormOptions.data.xmztqrsj = null;
            this.FormOptions.data.skzt = 'N';
            this.FormOptions.data.skqrr = null;
            this.FormOptions.data.skqrsj = null;
            this.FormOptions.data.kpzt = 'N';
            this.FormOptions.data.kpqrr = null;
            this.FormOptions.data.kpqrsj = null;
            this.FormOptions.data.zsh = null;
            this.FormOptions.data.zsksrq = null;
            this.FormOptions.data.zsjsrq = null;
            this.FormOptions.data.zslb = null;
            this.GetLcmbByTxid()            
          }
          this.editModal = true;
        }).catch(err => {
          VxeUI.modal.message({ content: `${err}`, status: 'error', duration: this.duration })
        })
    },
    GetLcmbByTxid(txid) {
      let request = {
        isPaging: false,
        pageSize: 1,
        pageIndex: 1,
        queryConditions: [{
          column:'gltxid',
          condition:6,
          content:`${txid || this.FormOptions.data.txid}`
        }],
        orderByConditions: []
      }
      CURDHelper.Query(request, '/api/xmjclcxxzb')
        .then(res => {
          this.lcmbItemRender.options = [];
          res.data.forEach(item => {
            this.lcmbItemRender.options.push({
              value: item.id,
              label: item.lcmbmc
            })
          })

        }).catch(err => {
          VxeUI.modal.message({content: `${err}`,status: 'error',duration: this.duration})
        })
    },
    GetTxOptions() {
      let request = {
        isPaging: false,
        pageSize: 1,
        pageIndex: 1,
        queryConditions: [],
        orderByConditions: []
      }
      CURDHelper.Query(request, '/api/Xmjcgltx')
        .then(res => {          
          res.data.forEach(item => {
            this.gltxItemRender.options = [];
            res.data.forEach(item => {
              this.gltxItemRender.options.push({
                value: item.id,
                label: item.txmc
              })
            })
            this.QueryConfig[2].chkoptions.push({
              label:item.id,
              value:item.id,
              content:item.txmc
            })
          })
        }).catch(err => {
          VxeUI.modal.message({content: `${err}`,status: 'error',duration: this.duration})
        })
    },
    ValidatorNumber(e) {
      if (!isNumber(e.itemValue)) {
        return new Error('请输入数字');
      } else {
        if (e.itemValue < 0) {
          return new Error('请输入大于等于0的数字');
        }
      }
    },
    handleEditModalSubmit(){
      this.DoAddData();
    },
    DoAddData() {
      //新增
      CURDHelper.Add(this.FormOptions.data, null, '/api/xmglxmxx')
        .then(res => {
          VxeUI.modal.message({ content: '新增项目信息成功', status: 'success', duration: this.duration })
          this.editModal = false;
        }).catch(err => {
          VxeUI.modal.message({ content: `${err}`, status: 'error', duration: this.duration })
        })
    },
    GetKhOptions() {
      let request = {
        isPaging: false,
        pageSize: 1,
        pageIndex: 1,
        queryConditions: [],
        orderByConditions: []
      }
      CURDHelper.Query(request, '/api/Xmjckhxx')
        .then(res => {
          this.khItemRender.options = [];
          res.data.forEach(item => {
            this.khItemRender.options.push({
              value: item.id,
              label: item.khmc
            })
          })
        }).catch(err => {
          VxeUI.modal.message({ content: `${err}`, status: 'error', duration: this.duration })
        })
    },
  }
}
